package com.wh.music.controller;

import com.github.pagehelper.Page;
import com.wh.music.POJO.Singer;
import com.wh.music.Util.ResultCode;
import com.wh.music.Util.ResultEntity;
import com.wh.music.Util.storage.StorageService;
import com.wh.music.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/singers")
public class SingerController {

  @Autowired
  private SingerService singerService;

  @Autowired
  private StorageService storageService;


  /**
   *  新增歌手
   * @param name
   * @param sex
   * @param pic
   * @param birth
   * @param location
   * @param introduction
   * @return   返回该歌手的主键值
   */
  @PostMapping(value = "/")
  public ResultEntity<Long> addSinger(@RequestParam("name") String name,
                                         @RequestParam("sex") String sex,
                                         @RequestParam("pic") String pic,
                                         @RequestParam("birth") String birth,
                                         @RequestParam("location") String location,
                                         @RequestParam("introduction") String introduction){
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date birthDate = null;
    try {
      birthDate = dateFormat.parse(birth);
    } catch (ParseException e) {
      e.printStackTrace();
      return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
    }
    //保存到歌手的对象中
    Singer singer = new Singer();
    singer.setName(name);
    singer.setSex(Integer.parseInt(sex));
    singer.setPic(pic);
    singer.setBirth(birthDate);
    singer.setLocation(location);
    singer.setIntroduction(introduction);
    int flag = singerService.insertsinger(singer);
    if(flag == 1){
      return ResultEntity.success(singer.getId());
    }
    return ResultEntity.failed(ResultCode.PARAM_TYPE_BIND_ERROR);
  }


  /**
   * 删除歌手
   */
  @DeleteMapping(value = "/{id}")
  public ResultEntity deleteSinger(@PathVariable("id") Long id){
    int flag = singerService.deletesinger(id);
    if(flag == 1) {
      return ResultEntity.success();
    }
    return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
  }

  @PostMapping("/batch_delete")
  public ResultEntity batchDelete(@RequestBody List<Long> singers) {
    int flag = singerService.deletesingers(singers);
    if(flag >= 1) {
      return ResultEntity.success();
    }
    return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
  }


  /**
   * 查询所有歌手
   */
  @GetMapping(value = "/")
  public ResultEntity<List<Singer>> allSinger(){
    List<Singer> res = singerService.queryAllSinger();
    return ResultEntity.success(res);
  }


  @GetMapping(value = "/{id}")
  public ResultEntity<Singer> selectByPrimaryKey(@RequestParam("id") Long id){
    Singer singer = singerService.queryByPrimaryKey(id);
    if(singer != null)
      return ResultEntity.success(singer);
    return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
  }


  /**
   * 根据歌手名字模糊查询列表
   */
  @GetMapping(value = "/{name}")
  public ResultEntity<List<Singer>> singerOfName(@RequestParam("name") String name){
    List<Singer> res = singerService.queryByName(name);
    if(res != null)
      return ResultEntity.success(res);
    return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
  }

  /**
   * 根据性别查询
   */
  @GetMapping(value = "/{sex}")
  public ResultEntity<List<Singer>> singerOfSex(@RequestParam("sex") Integer sex){
    List<Singer> res = singerService.queryBySex(sex);
    if(res != null)
      return ResultEntity.success(res);
    return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
  }

  @GetMapping(value = "/query")
  public ResultEntity<List<Singer>> PaginationSinger(@RequestParam("pageNo") Integer pageNo,
                                                     @RequestParam("pageSize") Integer pageSize,
                                                     @RequestParam("name") String name){
    Page<Singer> res = singerService.findByPage(pageNo, pageSize, name);
    if(res != null)
      return ResultEntity.success(res);
    return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
  }

  @GetMapping(value = "/sizes")
  public ResultEntity<Integer> totalSinger(@RequestParam("name") String name){
    Integer res = singerService.countSingers(name);
    if(res != null)
      return ResultEntity.success(res);
    return ResultEntity.failed(ResultCode.PARAM_IS_INVALID);
  }


  /**
   * 修改歌手
   */
  @RequestMapping(value = "/updateSinger",method = RequestMethod.POST)
  public Object updateSinger(@RequestParam("id") Integer id,
                             @RequestParam("name") String name,
                             @RequestParam("sex") String sex,
                             @RequestParam("pic") String pic,
                             @RequestParam("birth") String birth,
                             @RequestParam("location") String location,
                             @RequestParam("introduction") String introduction){
    //把生日转换成Date格式
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date birthDate = null;
    try {
      birthDate = dateFormat.parse(birth);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    //保存到歌手的对象中
    Singer singer = new Singer();
    singer.setId(id);
    singer.setName(name);
    singer.setSex(Integer.parseInt(sex));
    singer.setPic(pic);
    singer.setBirth(birthDate);
    singer.setLocation(location);
    singer.setIntroduction(introduction);
    Integer flag = singerService.updatesinger(singer);
    if(flag == 1){   //保存成功
      return ResultEntity.success();
    }
    return ResultEntity.failed(ResultCode.PARAM_IS_BLANK);
  }

  @PostMapping("/uploadPic")
  public ResultEntity<String> handleFileUpload(@RequestParam("uploadPic") MultipartFile file) {
    String filename = storageService.store(file);
    return ResultEntity.success(filename);
  }


}
